package cn.kgc.controller;

import cn.kgc.pojo.Standards;
import cn.kgc.service.StandardsService;
import cn.kgc.tools.Constants;
import cn.kgc.tools.PageSupport;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/user")
public class StandardsController {

    @Resource
    private StandardsService standardsService;

    @RequestMapping(value = "/show.html")
    public String show() {
        System.out.println("欢迎使用系统");
        return "Show";
    }

    // 分页展示
    @RequestMapping(value = "/userlist.html")
    @ResponseBody
    public Object getList(@RequestParam(value = "zhname",required = false) String zhname,
                          @RequestParam(value = "pageIndex",required = false) String pageIndex){
        System.out.println("zhname========"+zhname);
        System.out.println("pageIndex========"+pageIndex);

        String zhnames=null;
        // 设置页面容量
        int pageSize = Constants.pageSize;
        System.out.println("页面容量pageSize============" + pageSize);
        // 当前页面
        int currentPageNo = 1;

        if(zhname!=null && zhname!=""){
            zhnames=zhname;
        }

        //设置当前页
        if (pageIndex != null) {
            try {
                currentPageNo = Integer.valueOf(pageIndex);
            } catch (NumberFormatException e) {
                return "redirect:/user/syserror.html";
            }
        }

        //总数量
        int totalCount=standardsService.totalCount(zhnames);
        System.out.println("totalCount总数量============"+totalCount);

        //总页数
        PageSupport pageSupport=new PageSupport();
        pageSupport.setCurrentPageNo(currentPageNo);
        pageSupport.setPageSize(pageSize);
        pageSupport.setTotalCount(totalCount);
        int totalPageCount=pageSupport.getTotalPageCount();
        System.out.println("totalPageCount总页数=========="+totalPageCount);

        //控制首页和尾页
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }

        List<Standards> list=standardsService.getListByLimit(zhnames,currentPageNo,pageSize);

        HashMap<String, Object> result = new HashMap<String, Object>();
        result.put("list",list);
        result.put("totalPageCount",totalPageCount);
        result.put("pageSupport",pageSupport);
        result.put("zhname",zhname);

        String json = JSONArray.toJSONString(result);
        System.out.println("json============" + json);

        return json;
    }

    // 跳转增加页面
    @RequestMapping(value = "/useradd.html")
    public String add() {
        return "Add";
    }


    // 增加保存
    @RequestMapping(value = "/addSave.html")
    @ResponseBody
    public Object addinfo(@RequestParam(value = "stdnum") String stdnum,
                          @RequestParam(value = "zhname") String zhname,
                          @RequestParam(value = "ver") String ver,
                          @RequestParam(value = "keyes") String keyes,
                          @RequestParam(value = "releasedate") String releasedate,
                          @RequestParam(value = "impldate") String impldate,
                          @RequestParam(value = "packagepath") String packagepath) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Standards standards=new Standards();
        standards.setStdnum(stdnum);
        standards.setZhname(zhname);
        standards.setVer(ver);
        standards.setKeyes(keyes);
        standards.setReleasedate(sdf.parse(releasedate));
        standards.setImpldate(sdf.parse(impldate));
        standards.setPackagepath(packagepath);
        boolean flag=false;
        HashMap<String, Object> map = new HashMap<String, Object>();
        if(standardsService.add(standards)==true){
            flag=true;
            System.out.println("增加成功！");
            map.put("success",flag);
        }else {
            flag=false;
            System.out.println("增加失败！");
            map.put("false",flag);
        }
        String json = JSONArray.toJSONString(map);
        return json;
    }

    //删除
    @RequestMapping(value="/del.html")
    @ResponseBody
    public Object del(@RequestParam(value="ids")String ids){
        boolean flag=false;
        String[] IDS=ids.split(",");
        List<Integer> list=new ArrayList<>();
        for(int i=0;i<IDS.length;i++){
            int id=Integer.parseInt(IDS[i]);
            list.add(id);
            flag=standardsService.delete(id);
        }
        //String json=JSON.toJSONString(flag);
        HashMap<String,Object> map=new HashMap();
        map.put("list",list);
        map.put("flag",flag);
        return map;
    }

    // 跳转修改页面
    @RequestMapping("/update.html")
    public String updateById(@RequestParam(value = "id") String id, HttpSession session) {
        System.out.println("id======="+id);
        Standards standards = standardsService.getListById(Integer.parseInt(id));
//        System.out.println("enterprise======="+standards.getDepartmentId());
        session.setAttribute("standards", standards);
        return "Update";
    }

    // ajax修改保存
    @RequestMapping(value = "/updateSave.html")
    @ResponseBody
    public Object updateInfo(@RequestParam String id, @RequestParam String stdNum, @RequestParam String zhname,
                             @RequestParam String version, @RequestParam String keys, @RequestParam String releaseDate,
                             @RequestParam String implDate,@RequestParam String packagePath) throws ParseException {
        HashMap<String, Object> map = new HashMap<String, Object>();
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Standards standards=new Standards();
        standards.setId(Long.parseLong(id));
        standards.setStdnum(stdNum);
        standards.setZhname(zhname);
        standards.setVer(version);
        standards.setKeyes(keys);
        standards.setReleasedate(sdf.parse(releaseDate));
        standards.setImpldate(sdf.parse(implDate));
        standards.setPackagepath(packagePath);
        boolean flag = false;
        if (standardsService.update(standards)) {
            flag = true;
            map.put("suc", flag);
            System.out.println("修改成功！");
        } else {
            flag = false;
            map.put("msg", flag);
            System.out.println("修改失败");
        }
        String json = JSONArray.toJSONString(map);
        return json;
    }

}
